var params = app.getRequestParams();

$(function(){
    app.request({
        url:'account/select',
        success:function (resp) {
            initPage(resp.data);
        }
    });
});

function initPage(accountData) {
    $('#fixtable_zu').fixgrid({
        noscroll:true,
        width:950,
        columns:[
            [
                {field:'serialnumber',title:'',width:30},
                {field:'o',title:'操作',width:60,formatter:function () {
                        return gridOpt('#fixtable_zu');
                    }},
            ],[
                {field:'outName',title:'<span class="red">*</span>转出账户',width:120,editor:'combobox',
                    getData:function(cell,callback){
                        return callback(accountData);
                    },comboboxData:function (that) {
                        return $(that).data();
                    }},
                {field:'inName',title:'<span class="red">*</span>转入账户',width:120,editor:'combobox',
                    getData:function(cell,callback){
                        return callback(accountData);
                    },comboboxData:function (that) {
                        return $(that).data();
                    }},
                {field:'price',title:'<span class="red">*</span>金额',width:120,editor:'decimal'},
                {field:'remarks',title:'备注',width:300,editor:'text'}
            ]
        ]
    });

    if(params.status==2){
        editable = false;
    }
    initButtons({service:'finTransOrder',htmlName:'fintrans',orderText:'资金转账',codePrefix:'ZZ',orderType:0,gridId:'#fixtable_zu'});

    $('textarea[name="order.remarks"]').width($('#fixtable_zu').width()-14);

    laydate.render({
        elem: '#order_date',value:app.getServerDate()
    });

    // 必须放在最后  所有代码要写在之前
    if(params.id){
        app.request({
            url:'finTransOrder/getOrder',
            data:{id:params.id},
            success:function (resp) {
                initEdit(resp.data);
            }
        });
    }else{
        createRow('#fixtable_zu');createRow('#fixtable_zu');createRow('#fixtable_zu');createRow('#fixtable_zu');createRow('#fixtable_zu');
    }
}

//编辑初始化
function initEdit(data){
    var order = data.order;
    var details = data.details;

    init_edit_buttons(order,'#fixtable_zu');

    $('#order_title').html('编辑资金转账单【ZZ'+order.code+'】');
    $('#order_date').val(app.time2str(parseInt(order.date)));
    $('textarea[name="order.remarks"]').val(order.remarks);

    for (var i=0 ; i<details.length ; i++){
        var detail = details[i];
        createRow('#fixtable_zu');
        var lastRowEle = $('#fixtable_zu .ctx-right .vui-table-row').last();
        lastRowEle.find('.vui-table-cell[field="outName"]').html(detail.outAccountName).data({value:detail.outAccountId,text:detail.outAccountName});;
        lastRowEle.find('.vui-table-cell[field="inName"]').html(detail.inAccountName).data({value:detail.inAccountId,text:detail.inAccountName});;
        lastRowEle.find('.vui-table-cell[field="price"]').html(app.div(detail.price,app.DIV));
        lastRowEle.find('.vui-table-cell[field="remarks"]').html(detail.remarks);
    }

}

//保存
function saveAction(saveType,obj){
    var data = app.serializeForm();

    $.each(orderDetails,function(i,row){
        for ( var n in row ){
            data["details["+i+"]."+n] = row[n];
            data["details["+i+"].ord"] = i;
        }
    });

    var url = 'finTransOrder/insert';
    if(params.id){
        data['order.id'] = params.id;
        url = 'finTransOrder/update';
    }

    data.saveType = saveType;
    doSave(url,data,saveType,obj);
}


//表单检查
var orderDetails = [],accounts=[];
function checkForm(){

    var checkOptions = [
        {type:'input',name:'order.date',verify:[{type:'required'}]}
    ];

    if(app.checkForm(checkOptions)){

        var tipFlag = false;

        orderDetails = [];
        $('#fixtable_zu .ctx-right .vui-table-row').each(function () {
            var detail = {};
            var outAcct = $(this).find('.vui-table-cell[field="outName"]').data();
            if( outAcct.value ){
                //账户id
                detail.outAccountId = outAcct.value;

                //
                var inAcct = $(this).find('.vui-table-cell[field="inName"]').data();
                if( !inAcct.value ){
                    tipFlag = true;
                    app.msg(layer,'请选择转入账户',2);
                    return false;
                }
                detail.inAccountId = inAcct.value;

                var price = $(this).find('.vui-table-cell[field="price"]').html();
                // 金额
                if( $.trim(price).length == 0 ){
                    tipFlag = true;
                    app.msg(layer,'请填写转账金额',2);
                    return false;
                }
                detail.price = app.mul(price,app.DIV,0);

                // 备注
                detail.remarks = $(this).find('.vui-table-cell[field="remarks"]').html();
                orderDetails[orderDetails.length] = detail;
            }
        });

        if(orderDetails.length > 0 && !tipFlag){
            return true;
        }else{
            if(!tipFlag){
                app.msg(layer,'至少填写一条收入记录',2);
                return false;
            }else{
                return false;
            }
        }
    }

    return false;
}